package com.lm.activiti.boot.design_pattern.factory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * @author lm_jcf
 * @date 2020/11/20 11:59
 */
//@Component("baseConstructor")
public class BaseConstructor {

    private BeanContext context;

    private ThreadLocal<BeanContext> beanContextThreadLocal = new ThreadLocal<>();

//    @Autowired
    private List<BeanConstructor> beanConstructorList;

    public BeanContext getContext(){
        return beanContextThreadLocal.get();
    }

    public void setContext(BeanContext context) {
        this.context = context;
        beanContextThreadLocal.set(context);
    }

    public void defineContext(BeanContext context){
        setContext(context);
    }

    public void createBean(){

        if(Objects.nonNull(context)){
            System.out.println(context.getName());
        }
    }

    public void construct(){

        BeanContext beanContext = new BeanContext();
        beanContext.setName("父类");
        setContext(beanContext);

        for (int i = 0; i < beanConstructorList.size(); i++) {

            BeanConstructor childConstructor = beanConstructorList.get(i);
            childConstructor.defineContext(beanContext);

            childConstructor.createBean();
        }

    }

    public void setBeanConstructorList(List<BeanConstructor> beanConstructorList) {
        this.beanConstructorList = beanConstructorList;
    }
}
